Strobogrammatic number¶
Time: O(N); Space: O(1); easy
A mirror number is a number that looks the same when rotated 180 degrees (looked at upside down).
For example, the numbers “69”, “88”, and “818” are all mirror numbers.
Write a function to determine if a number is mirror. The number is represented as a string.
Have you met this question in a real interview?
Example 1:
Input: num = “69”
Output: True
Example 2:
Input: num = “68”
Output: False
[3]:
class Solution1(object):
"""
Time: O(N)
Space: O(1)
"""
lookup = {'0':'0', '1':'1', '6':'9', '8':'8', '9':'6'}
def isStrobogrammatic(self, num):
"""
:type num: str
:rtype: bool
"""
n = len(num)
for i in range((n+1) // 2):
if num[n-1-i] not in self.lookup or \
num[i] != self.lookup[num[n-1-i]]:
return False
return True
[4]:
s = Solution1()
num = "69"
assert s.isStrobogrammatic(num) == True
num = "68"
assert s.isStrobogrammatic(num) == False